package project.ccso.top.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.org.apache.regexp.internal.recompile;

import project.ccso.top.dao.OneCategoryDao;
import project.ccso.top.entity.OneCategory;
import project.ccso.top.entity.Province;
import project.ccso.top.util.DBUtil;
import project.ccso.top.util.DateUtil;

/**
 * 一级分类Dao的实现类
 * @author aibinxiao
 * @date 2018年8月23日 上午10:41:13
 */
public class OneCategoryDaoImpl implements OneCategoryDao{
	// 日志
	private static Logger logger = LoggerFactory.getLogger(OneCategoryDaoImpl.class);
	
	private DBUtil dbUtil =new DBUtil();
	private ResultSet rs = null;
	private int Result = -1;
	private OneCategory oneCategory = null;
	
	@Override
	public OneCategory findCateById(int id) {
		// TODO Auto-generated method stub
		String sql ="select * from t_onecategory where one_cate_id =?";
		rs = dbUtil.executeQuery(sql, new Object[] {id});
		try {
			rs.next();
			oneCategory = new OneCategory(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getString(6),rs.getString(7));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			oneCategory  = null;
		}
		return oneCategory;
	}

	@Override
	public int countByParams(String id, String name, String code, String startDate, String endDate) {
		// TODO 条件查询符合条件的一级分类记录条数Dao实现类
		List<Object> objects = new ArrayList<>();
		int count = 0;
		String sql = "select count(*) from t_onecategory where 1=1";
		if(!StringUtils.isEmpty(id)){
			sql += " and one_cate_id = ?";
			objects.add(id);
		}
		if(!StringUtils.isEmpty(name)){
			sql += " and one_cate_name = ?";
			objects.add(name);
		}
		if(!StringUtils.isEmpty(code)){
			sql += " and one_cate_code = ?";
			objects.add(code);
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			sql += " and one_cate_create_time between ? and ?";
		}
		ResultSet rs = dbUtil.executeQuery(sql, objects);
		try {
			if(rs.next()) {
				count = rs.getInt(1);
			}
			logger.info("找到("+ count +")个符合查询条件的一级分类信息");
			return count;
		} catch (SQLException e) {
			logger.info("没有找到符合查询条件的一级分类信息");
			e.printStackTrace();
			return count;
		}
	}

	@Override
	public List<OneCategory> findPageByParams(String id, String name, String code, String startDate, String endDate,
			int startIndex, int records) {
		// TODO 分页查询符合条件的一级分类记录Dao实现类
		List<OneCategory> result = new ArrayList<>();
		List<Object> objects = new ArrayList<>();
		String sql = "select * from t_onecategory where 1=1";
		if(!StringUtils.isEmpty(id)){
			sql += " and one_cate_id = ?";
			objects.add(id);
		}
		if(!StringUtils.isEmpty(name)){
			sql += " and one_cate_name like ?";
			objects.add("%" + name + "%");
		}
		if(!StringUtils.isEmpty(code)){
			sql += " and one_cate_code = ?";
			objects.add(code);
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			sql += " and one_cate_create_time between ? and ?";
		}
		ResultSet rs = dbUtil.executeQuery(sql, objects);
		try {
			while(rs.next()) {
				oneCategory = new OneCategory(rs.getInt(1), rs.getString(2),rs.getString(3),rs.getString(4) ,rs.getInt(5), rs.getString(6), rs.getString(7));
				result.add(oneCategory);
			}
		} catch (SQLException e) {
			result = null;
		}
		return result;
	}

	@Override
	public boolean UpdateCateById(String id, String code, String name, String desc) {
		boolean flag = false;
		String sql = "";
		int count = 0;
		if(StringUtils.isEmpty(id)) {
			// 添加操作
			sql = "insert into t_onecategory (one_cate_code,one_cate_name ,one_cate_desc,one_cate_status,one_cate_create_time,one_cate_alter_time)values (?,?,?,?,?,?)";
			count = dbUtil.executeUpdate(sql, new Object[] {code,name,desc,0, new Date(), new Date()});
			if(count != -1) {
				flag = true;
				logger.info("名称为" + name + "的一级分类信息添加成功！");
			}
		}else {
			// 修改操作
			int idin = 1;
			try {
				idin = Integer.parseInt(id);
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("参数id不是整数类型或超出int范围！");
			}
			sql = "update t_onecategory set one_cate_code = ?,one_cate_name = ?,one_cate_desc = ? , one_cate_alter_time = ? where one_cate_id = ?";
			count = dbUtil.executeUpdate(sql, new Object[] {code,name,desc,new Date(), id});
			if(count != -1) {
				flag = true;
				logger.info("id为" + idin + "的一级分类信息修改成功！");
			}
		}
		return flag;
	}
	
	/**
	 * 1.获取所有启用的商品一级分类信息集合
	 * @return
	 *//*
	@Override
	public List<OneCategory> findAllOneCategory() {
		String hql = "from OneCategory where status = ?";
		List<OneCategory> result = null;
		List<OneCategory> list = this.getHibernateTemplate().find(hql, 0);
		if(list != null && list.size() > 0){
			logger.info("所有商品一级分类信息集合：" + list);
			result = list;
		}
		return result;
	}

	// 2.分页条件查询符合条件的记录条数
	@Override
	public int countByParams(String id, String name, String code, String startDate, String endDate) {
		int count = 0;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(OneCategory.class);
		if(!StringUtils.isEmpty(id)){
			Integer oneCategoryId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(oneCategoryId));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(code)){
			criteria.add(Restrictions.like("code", code, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("createTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<OneCategory> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list != null && list.size() > 0) {
			count = list.size();
			logger.info("找到("+ list.size() +")个符合查询条件的一级商品分类");
		} else{
			logger.info("没有找到符合查询条件求的一级商品分类");
		}
		return count;
	}

	// 3.分页查询指定页数的所有记录
	@Override
	public List<OneCategory> findPageByParams(String id, String name, String code, String startDate, String endDate,
			int startIndex, int records) {
		List<OneCategory> result = null;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(OneCategory.class);
		if(!StringUtils.isEmpty(id)){
			Integer oneCategoryId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(oneCategoryId));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(code)){
			criteria.add(Restrictions.like("code", code, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("createTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<OneCategory> list = this.getHibernateTemplate().findByCriteria(criteria, startIndex, records);
		if (list != null && list.size() > 0) {
			result = list;
			logger.info("找到("+ list.size() +")个符合查询条件的一级商品分类");
		} else{
			logger.info("没有找到符合查询条件求的一级商品分类");
		}
		return result;
	}

	// 4.根据ID查询一级分类Dao实现类
	@Override
	public OneCategory findById(Integer id) {
		String hql = "from OneCategory where id = ?";
		OneCategory result = null;
		List<OneCategory> list = this.getHibernateTemplate().find(hql, id);
		if(list != null && list.size() > 0){
			logger.info("根据ID:"+id+",查询到一级分类:" + list.get(0));
			result = list.get(0);
		}
		return result;
	}

	// 5.保存或者更新一级分类信息Dao实现类
	@Override
	public void saveOneCategory(OneCategory oneCategory) throws Exception {
		this.getHibernateTemplate().saveOrUpdate(oneCategory);
	}

	// 6.更新三级分类信息状态Dao实现类
	@Override
	public void updateStauts(Integer id, Integer statusCode) throws Exception {
		String hql = "from OneCategory where id = ?";
		List<OneCategory> list = this.getHibernateTemplate().find(hql, id);
		if(list!=null && list.size()>0){
			OneCategory oneCategoty = list.get(0);
			oneCategoty.setStatus(statusCode);
			oneCategoty.setAlterTime(DateUtils.DateToStr(new Date()));
			this.getHibernateTemplate().saveOrUpdate(oneCategoty);
		}
	}*/
}
